<?php
namespace app\manage\controller;
use think\Db;

class Index extends Base
{
    public function index(){
        //获取该用户的权限（这里可以从缓存读取）
        $whereRole['id'] = session('admin.role_id');
        $priIdList = model('SystemRole')->where($whereRole)->value('pri_id_list_new'); //priList为1代表超级管理员

        //获取所有菜单
        $whereMenu['status'] = 1;
        $priIdList!=='#' && $whereMenu['pri_id'] = ['in', $priIdList];
        $menuList = model('SystemMenuNew')->where($whereMenu)->order('sort desc')->select();

        //显示有该权限的菜单
        $topMenu = [];
        foreach ($menuList as $k=>$v){
            if( $v['pid'] == 1 ){
                $topMenu[] = [
                    'id'=> $v['id'],
                    'title'=> $v['title'],
                    'icon'=> $v['icon'],
                    'href' => $v['url'],
                ];
            }
        }

        import('cate.Category');
        $leftMenu = [];
        foreach ($topMenu as $k=>$v){
            $leftMenu[] = [
                'id' => $v['id'],
                'data' => \Category::unlimitedForLayer($menuList, $name='children', $v['id']),
            ];
        }

        $this->assign('topMenu', $topMenu);
        $this->assign('leftMenu', $leftMenu);
        return view();
    }

    //验证是否有欢迎页权限
    public function welcome2(){
        if( $this->containPri('Index/welcome') ){
            return view('welcome2'); ///////////////welcome
        }else{
            return view();
        }
    }

    public function today(){
        $startTime = strtotime(date('Y-m-d', time()));
        $endTime = time();

        $year = date('Y', $startTime);
        $month = date('Ym', $startTime);

        $wherePlat = "reg_time between {$startTime} AND {$endTime}";
        $whereAdd = "bond_time between {$startTime} AND {$endTime}";
        $whereActive = "in_time between {$startTime} AND {$endTime}";
        $whereOrder = "pay_time between {$startTime} AND {$endTime} AND debug=0 AND pay_result=1";

        $selectActive = Db::name('GameUserLogM_'.$month)->field("count(DISTINCT(my_user_id)) AS game_active")->where($whereActive)->group('game_id')->select();
        $totalPrice = Db::name('PayOrderY_'.$year)->where($whereOrder)->sum('total_price');
        $todaySum['plat_add'] = Db::name('User')->where($wherePlat)->count();
        $todaySum['game_add'] = Db::name('GameUserY_'.$year)->where($whereAdd)->count();
        $todaySum['game_active'] = sumCount($selectActive, 'game_active');
        $todaySum['pay_money'] = round($totalPrice/100, 0);
        return $todaySum;
    }

    public function week(){
        //过去七天平均值
        $week1 = strtotime(date('Y-m-d', strtotime("-7 day", time())));
        $week2 = strtotime(date('Y-m-d', time()))-1;

        $where = "daytime between {$week1} AND {$week2}";

        $sum = Db::name('DataBuffer')->field('sum(plat_add) as plat_add, sum(game_add) as game_add, sum(game_active) as game_active, sum(pay_money) as pay_money')->where($where)->find();

        $oldSum['plat_add'] = round($sum['plat_add']/7, 0);
        $oldSum['game_add'] = round($sum['game_add']/7, 0);
        $oldSum['game_active'] = round($sum['game_active']/7, 0);
        $oldSum['pay_money'] = round($sum['pay_money']/7, 0);
        return $oldSum;
    }

    public function lock(){
        return view();
    }

    public function feedback(){
        return view();
    }

    public function history(){
        return view();
    }

    public function quit(){
        session('admin', null);
        $this->redirect('Show/login');
    }
}
